package com.wedimob.slideshare.bo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.wedimob.slideshare.parser.annotation.Factory;
import com.wedimob.slideshare.parser.annotation.HTMLMapping;
import com.wedimob.slideshare.parser.annotation.DocMappings;
import com.wedimob.slideshare.source.SlideShareStrategy;

public class Author extends AbstractBO<Author> {
    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.nickname"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "TXT[{\"login\":\",\"]", attr = "content"),
            })
    private String username;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.h-name"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=og:title]", attr = "content"),
            })
    private String name;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.role"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=og:work]", attr = "content"),
            })
    private String title;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.org"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=slideshare:organization]", attr = "content"),
            })
    private String organization;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.adr"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=slideshare:location]", attr = "content"),
            })
    private String location;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.about"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=og:description]", attr = "content"),
            })
    private String resume;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "span.user-joined-time"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=slideshare:joined_on]", attr = "content"),
            })
    private Calendar registrationDate;

    @DocMappings(
            html =
            {
                    @HTMLMapping(url = SlideShareStrategy.SEARCH_PATTERN, tag = "img.photo", attr = "src"),
                    @HTMLMapping(url = SlideShareStrategy.USER_PAGE_PATTERN, tag = "meta[property=og:image]", attr = "content"),
            })
    private Medium picture;

    @HTMLMapping(url = SlideShareStrategy.USER_DOC_PAGE_PATTERN, tag = "li[id]")
    private Summaries publications;

    public Author() {
        super();
    }

    public Author(String name, String username, String title, String organization, String location, String resume,
            Calendar registrationDate, Medium picture) {
        super();
        this.name = name;
        this.username = username;
        this.title = title;
        this.organization = organization;
        this.location = location;
        this.resume = resume;
        this.registrationDate = registrationDate;
        this.picture = picture;
        publications = new Summaries();
    }

    public Summaries getPublications() {
        return publications;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getResume() {
        return resume;
    }

    public void setResume(String resume) {
        this.resume = resume;
    }

    public Calendar getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Calendar registrationDate) {
        this.registrationDate = registrationDate;
    }

    public Medium getPicture() {
        return picture;
    }

    public void setPicture(Medium picture) {
        this.picture = picture;
    }

    public URL getURL() {
        try {
            return new URL(SlideShareStrategy.URL_BASE + "/" + username);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public URL getSlidesURL() {
        try {
            return new URL(SlideShareStrategy.URL_BASE + "/" + username + "/slideshelf");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        Document doc = Jsoup.connect("http://fr.slideshare.net/Nubiagroup").timeout(60000).get();
        Author author = Factory.fromDocument(doc, Author.class, null);
        doc = Jsoup.connect(author.getSlidesURL().toString()).timeout(60000).get();
        author = Factory.fromDocument(doc, Author.class, author);
        System.out.println(author);
    }
}
